import pymysql
from pdfminer.pdfpage import PDFParser,PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import LTTextBoxHorizontal,LAParams
from pdfminer.pdfpage import PDFTextExtractionNotAllowed
import threading
from win32com.client.dynamic import Dispatch,ERRORS_BAD_CONTEXT
import os
import PyPDF2
#使用PyPDF2实现
def pdf2txt(pdffile,save_path):
    try:
        with open(pdffile,'rb') as f:
            pdf_reader = PyPDF2.PdfReader(pdffile)
            num_pages = len(pdf_reader.pages)
            # print(pdf_reader.pages)
            page_text = ''
            for page_number in range(num_pages):
                page_obj = pdf_reader.pages[page_number]
                page_text += page_obj.extract_text()
            # print(page_text)
        with open(save_path, 'w', encoding='utf-8') as txt_file:
            txt_file.write(page_text)
        return page_text
    except:
        return "转换错误"
def acrobat2txt(f_path,d_path):
    # try:
        AvDoc=Dispatch("Word.Application")
        print(1)
        print(f_path)
        mytxt=AvDoc.Documents.Open(f_path)
        print(2)
        mytxt.SaveAs(d_path,4)
        print(3)
        mytxt.Close()
        print(4)
        # jsObject.SaveAs(d_path,"com.adobe.acrobat.plain-text")
        # #jsObject.SaveAs(d_path, "com.adobe.acrobat.docx")
        # print(5)
        print("OK")
    # except Exception as e:
        print('error')
        # print(e)
    # finally:
        AvDoc.close(True)

def upload2sql():
    pass
if __name__=="__main__":

    pass
    # pdf='12125333632021年社会责任报告.pdf'
    # savepath='12125333632021年社会责任报告.txt'
    # pdf2txt(pdf,savepath)